package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetailBean;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetails;
import com.ciwong.epaper.modules.epaper.ui.ChooseWordActivity;
import com.ciwong.epaper.modules.me.bean.FollowReadWordDetailBeans;
import com.ciwong.mobilelib.widget.NoScrollGridView;

import java.util.List;

public class ChooseWordExAdapter extends BaseExpandableListAdapter {
    private FollowReadWordDetailBeans followReadWordDetailBeans ;
    private Context mContext;
    private ChooseWordGridAdapter chooseWordGridAdapter;
    private MediaPlayer mediaPlayer;

    public ChooseWordExAdapter(FollowReadWordDetailBeans followReadWordDetailBeans,Context mContext,MediaPlayer mediaPlayer){
        this.followReadWordDetailBeans = followReadWordDetailBeans;
        this.mContext = mContext;
        this.mediaPlayer = mediaPlayer;
    }

    @Override
    public int getGroupCount() {
        return 0 == followReadWordDetailBeans.getFollowReadWordDetailBeanList().size() ? 0 : followReadWordDetailBeans.getFollowReadWordDetailBeanList().size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 0 == followReadWordDetailBeans.getFollowReadWordDetailBeanList().size() ? 0 : 1;
    }

    @Override
    public Object getGroup(int i) {
        return 0 == followReadWordDetailBeans.getFollowReadWordDetailBeanList().size() ? null : followReadWordDetailBeans.getFollowReadWordDetailBeanList().get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return 0 == followReadWordDetailBeans.getFollowReadWordDetailBeanList().size() ? null : followReadWordDetailBeans.getFollowReadWordDetailBeanList().get(i).getFollowReadWordDetailsList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ViewHolderGroup viewHolderGroup;
        if (null == view){
            view = LayoutInflater.from(mContext).inflate( R.layout.activity_choose_word_ex_list_parent,viewGroup,false);
            viewHolderGroup = new ViewHolderGroup();
            viewHolderGroup.choose_word = (CheckBox) view.findViewById(R.id.choose_word);
            viewHolderGroup.choose_word_text = (TextView) view.findViewById(R.id.choose_word_text);
            view.setTag(viewHolderGroup);
        }else {
            viewHolderGroup =(ViewHolderGroup) view.getTag();
        }
        if (i == 0){
            viewHolderGroup.choose_word_text.setText("第"+1+"-"+10+"个单词");
        }
        viewHolderGroup.choose_word_text.setText("第"+((i*10)+1)+"-"+((i+1)*10)+"个单词");
        final int checkGroupCount = followReadWordDetailBeans.getCheckGroupCount();
        int groupSize = followReadWordDetailBeans.getFollowReadWordDetailBeanList().size();

        final FollowReadWordDetailBean followReadWordDetailBean = followReadWordDetailBeans.getFollowReadWordDetailBeanList().get(i);
        //设置选中状态
            if (followReadWordDetailBean.getFollowReadWordDetailsList().size() == followReadWordDetailBean.getCheckCount()){
                viewHolderGroup.choose_word.setChecked(true);
                viewHolderGroup.choose_word.setSelected(true);
            }else if (0 == followReadWordDetailBean.getCheckCount()){
                viewHolderGroup.choose_word.setChecked(false);
                viewHolderGroup.choose_word.setSelected(true);
            }else {
                viewHolderGroup.choose_word.setChecked(true);
                viewHolderGroup.choose_word.setSelected(false);
            }
        viewHolderGroup.choose_word.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (b){
                    viewHolderGroup.choose_word.setChecked(true);
                    viewHolderGroup.choose_word.setSelected(true);
                    followReadWordDetailBeans.setCheckGroupCount(checkGroupCount+1);
                    List<FollowReadWordDetailBean> ff = followReadWordDetailBeans.getFollowReadWordDetailBeanList();
                    /*for (int i1 = 0; i1 < ff.size(); i1++) {
                        List<FollowReadWordDetails> followReadWordDetailsList = ff.get(i1).getFollowReadWordDetailsList();
                        for (int i2 = 0; i2 < followReadWordDetailsList.size(); i2++) {
                            followReadWordDetailsList.get(i2).setIsChecked(true);
                        }
                    }*/
                    List<FollowReadWordDetails> followReadWordDetailsList = followReadWordDetailBean.getFollowReadWordDetailsList();
                    for (int i1 = 0; i1 < followReadWordDetailsList.size(); i1++) {
                        followReadWordDetailsList.get(i1).setIsChecked(true);
                    }
                    followReadWordDetailBean.setCheckCount(followReadWordDetailBean.getFollowReadWordDetailsList().size());
                    if (mContext instanceof ChooseWordActivity){
                        ((ChooseWordActivity)mContext).setCheckStatus();
                    }
                    if (mContext instanceof ChooseWordActivity){
                        ((ChooseWordActivity)mContext).setChooseWordDetail();
                    }
                    ChooseWordExAdapter.this.notifyDataSetChanged();
                }else {
                    viewHolderGroup.choose_word.setChecked(false);
                    viewHolderGroup.choose_word.setSelected(true);
                    followReadWordDetailBeans.setCheckGroupCount(checkGroupCount - 1);
                    List<FollowReadWordDetails> followReadWordDetailsList = followReadWordDetailBean.getFollowReadWordDetailsList();
                    for (int i1 = 0; i1 < followReadWordDetailsList.size(); i1++) {
                        followReadWordDetailsList.get(i1).setIsChecked(false);
                    }
                    followReadWordDetailBean.setCheckCount(0);
                    if (mContext instanceof ChooseWordActivity){
                        ((ChooseWordActivity)mContext).setCheckStatus();
                        ((ChooseWordActivity)mContext).setChooseWordDetail();
                    }
                    ChooseWordExAdapter.this.notifyDataSetChanged();
                }
            }
        });
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (null == view){
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_choose_word_ex_list_child,viewGroup,false);
        }
        NoScrollGridView choose_word_grid = (NoScrollGridView) view.findViewById(R.id.choose_word_grid);
        chooseWordGridAdapter = new ChooseWordGridAdapter(followReadWordDetailBeans.getFollowReadWordDetailBeanList().get(i),mContext,mediaPlayer,ChooseWordExAdapter.this);
        choose_word_grid.setAdapter(chooseWordGridAdapter);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private static class ViewHolderGroup{
        private CheckBox choose_word;
        private TextView choose_word_text;
    }

    public void stopPlayAnimation(){
        //chooseWordGridAdapter.stopPlayAnimation();
    }

    public void setCheckStatus(Boolean isChecked){
        int checkGroupCount = followReadWordDetailBeans.getCheckGroupCount();
        if (isChecked){
            followReadWordDetailBeans.setCheckGroupCount(checkGroupCount + 1);
        }else {
            followReadWordDetailBeans.setCheckGroupCount(checkGroupCount - 1);
        }
        if (mContext instanceof ChooseWordActivity){
            ((ChooseWordActivity)mContext).setCheckStatus();
        }
        ChooseWordExAdapter.this.notifyDataSetChanged();
    }

}
